Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartid:null,
    arr:[],
    num:0,
    show:true,
    address:[],
    pay:false,
    Leave:"",
    pass: false,
    word:null,
    orserid:null,
    if_0:true,
    if_00:null,
    shu:0,
    oName:"",
    oImg:"",
    // 
    payment_mode: 1,//默认支付方式 微信支付
    isFocus: false,//控制input 聚焦
    balance: 100,//余额
    actual_fee: 20,//待支付
    wallets_password_flag: false//密码输入遮罩
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id);
    this.setData({
      if_00: options.oID,
    })
    
    if (options.oID == "2,3" || options.oID == "1,3"){
      this.setData({
        goodID: options.id,        
        if_0: true,
      })
      var that = this;
      wx.request({
        url: 'https://bag.idianwei.com/index.php/Api/Api/repackList',
        data: {
          // goodsType: "2,3",
          // page: 1,
          // row: 10,
        },
        success: res => {
          
          console.log(res);
          for (var i = 0; i < res.data.resultObj.length;i++){
            if (res.data.resultObj[i].goodsId==that.data.goodID){
              that.setData({
                arr: res.data.resultObj[i],
                oImg: res.data.resultObj[i].goodsImg,
                oName: res.data.resultObj[i].goodsName,
                Spec: res.data.resultObj[i].goodsSpec,
                shopprice: res.data.resultObj[i].shopPrice,
                rentPrice: res.data.resultObj[i].rentPrice,
              })
              if (options.oID == "1"){
                that.setData({
                  shu:10,
                });
                var zong = that.data.shu * that.data.rentPrice;
                that.setData({
                  num:zong,
                })
              } else if (options.oID == "2"){
                that.setData({
                  num: that.data.shopprice,
                });
              }
            }
          }
          
        }
      })



      
      wx.request({
        url: "https://bag.idianwei.com/index.php/Api/User/deAddress",
        data: {
          userId: 16,
          token: 1501221705,
        },
        success: (res) => {
          console.log(res);
          this.setData({
            address: res.data.resultObj,
          });
          if (res.data.resultObj.length == undefined || res.data.resultObj.length == 0) {
            this.setData({
              show: false,
            })
          } else {
            this.setData({
              show: true,
            })
          }
        }
      })

    } else if (options.oID == undefined){
      var bean = JSON.parse(options.id);
      this.data.arr.push(bean);
      wx.request({
        url: "https://bag.idianwei.com/index.php/Api/User/deAddress",
        data: {
          userId: 16,
          token: 1501221705,
        },
        success: (res) => {
          console.log(res);
          this.setData({
            address: res.data.resultObj,
          });
          if (res.data.resultObj.length == undefined || res.data.resultObj.length == 0) {
            this.setData({
              show: false,
            })
          } else {
            this.setData({
              show: true,
            })
          }
        }
      })

      this.setData({
        if_0: false,
      })
      console.log(bean.data);
      this.setData({
        arr:bean.data
      })
      console.log(this.data.arr);console.log(this.data.arr);
      var zong=0;
      for (var i = 0; i < this.data.arr.length; i++) {
        zong += parseInt(this.data.arr[i].goodsCnt) * parseInt(this.data.arr[i].goodsPrice);
      }
      this.setData({
        num:zong,
      })
    }




    this.setData({
      Leave: '',
    })

  },
  
  // 买家留言
  Leave:function(e){
    console.log(e.detail.value.length);
    if (e.detail.value.length == 0){
      this.setData({
        Leave: '',
      })
    }else{
      this.setData({
        Leave: e.detail.value,
      })
    }
    
    
  },

  ress:function(){
    wx.navigateTo({
      url: '../set_1/set',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // wx.request({
    //   url: "https://bag.idianwei.com/index.php/Api/Api/cartList",
    //   data: {
    //     userId: 22,
    //     token: 1516268418139,
    //   },
    //   success: (res) => {
    //     console.log(res);
    //     console.log(this.data.cartid);
    //     var zong=0;
    //     for (var i = 0; i < res.data.resultObj.length; i++) {
    //       zong += parseInt(res.data.resultObj[i].goodsCnt) * parseInt(res.data.resultObj[i].goodsPrice);
    //     }
    //     this.setData({
    //       arr: res.data.resultObj,
    //       num: zong,
    //     })
    //     // console.log(zong);
    //   }
    // })
  },
  // 下单
  pay00:function(){
    console.log(this.data.oImg);
    var Num = this.data.num;
    if (this.data.pay==false){
      this.setData({
        pay:true,
      })
    }else{
      this.setData({
        pay: true,
      })
    }
    var cart=this.data.cartid;
    if (this.data.if_00 == "1" || this.data.if_00 == "2"){
      console.log(this.data.Leave);
      var a=1;
      var b=1;
      
      if(this.data.if_00 == "2"){
        wx.request({
          url: "https://bag.idianwei.com/index.php/Api/Api/makeOrder",
          data: {
            userId: 16,
            token: 1501221705,
            addressId: this.data.address.addressId,
            is_sele: this.data.if_00,
            orderRemarks: this.data.Leave,
            couponId: "",
            couponMoney: "",
            totalMoney: Num,
            realTotalMoney: Num,
            orderFrom: 1,
            goodsImg1: this.data.oImg,
            goodsId: this.data.goodID,
            goodsNums: 1,
            goodsPrice: this.data.shopprice,
            goodsName: this.data.oName,
          },
          success: (res) => {
            console.log(res);
            this.setData({
              orserid: res.data.resultObj.orderId,
            });
            console.log(this.data.num + "a");
          }
        })
      } else if(this.data.if_00 == "1"){
        console.log(a);
        
        wx.request({
          url: "https://bag.idianwei.com/index.php/Api/Api/makeOrder",
          data: {
            userId: 16,
            token: 1501221705,
            addressId: this.data.address.addressId,
            orderRemarks: this.data.Leave,
            is_sele: this.data.if_00,
            couponId: "",
            couponMoney: "",
            totalMoney: Num,
            realTotalMoney: Num,
            orderFrom: 1,
            goodsImg1: this.data.oImg,
            zu_lenth: 10,
            zu_money: this.data.rentPrice,
            goodsId: this.data.goodID,
            goodsName: this.data.oName,
          },
          success: (res) => {
            console.log(res);
            this.setData({
              orserid: res.data.resultObj.orderId,
            });
          }
        })
      }
    }else{
      console.log(this.data.arr);
      var card="";
      for(var i=0;i<this.data.arr.length;i++){
        console.log(this.data.arr[i].cartId);
        card += this.data.arr[i].cartId+",";
      }
      var card1 = card.substr(0, card.length - 1);
      console.log(card1);
      wx.request({
        url: "https://bag.idianwei.com/index.php/Api/Api/payCart",
        data: {
          userId: 16,
          token: 1501221705,
          cartId: card1,
          addressId: this.data.address.addressId,
          orderRemarks: this.data.Leave,
          couponId: "",
          couponMoney: "",
          totalMoney: 1,
          realTotalMoney: 1,
          orderFrom: 1,
        },
        success: (res) => {
          // console.log(this.data.address.addressId+"a1");
          // console.log(card1 + "a2");
          // console.log(this.data.Leave + "a3");
          // console.log(this.data.num + "a4")
          console.log(res);
          //获取订单号
          this.setData({
            orserid: res.data.resultObj.orderId,
          });
        }
      })
    }
    
  },

  // 支付
  pay_ren:function(){
    if(this.data.pass==false){
      this.setData({
        pass:true,
      })
    }else{
      this.setData({
        pass: false,
      })
    }
    console.log(this.data.orserid);
  },
  // 密码输入框
  // password:function(e){
  //   console.log(e.detail.value);
  //   this.setData({
  //     word: e.detail.value,
  //   });
  //   console.log(this.data.word);
    
  // },
  // ti:function(){
  //   wx.request({
  //     url: 'https://bag.idianwei.com/index.php/Api/Api/payOrder',
  //     data: {
  //       userId: 22,
  //       token: 1516268418139,
  //       orderId: this.data.orserid,
  //       payPwd: 123456,
  //     },
  //     success:(res)=>{
  //       console.log(res);
  //     }
  //   })
  // },
  //隐藏支付样式
  // cha:function(){
  //   if (this.data.pay == true) {
  //     this.setData({
  //       pay: false,
  //     })
  //   } else {
  //     this.setData({
  //       pay: true,
  //     })
  //   }
  // },
  password:function(e){
    console.log(e)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  wx_pay() {//转换为微信支付
    this.setData({
      payment_mode: 1
    })
  },
  offline_pay() {//转换为转账支付
    this.setData({
      payment_mode: 0
    })
  },
  wallet_pay() {
    this.setData({//转换为钱包支付
      payment_mode: 2
    })
  },
  set_wallets_password(e) {//获取钱包密码
    this.setData({
      wallets_password: e.detail.value
    });
    if (this.data.wallets_password.length == 6) {//密码长度6位时，自动验证钱包支付结果
      wallet_pay(this)
      wx.request({
        url: 'https://bag.idianwei.com/index.php/Api/Api/payOrder',
        data: {
          userId: 16,
          token: 1501221705,
          orderId: this.data.orserid,
          payPwd: this.data.wallets_password,
        },
        success: (res) => {
          console.log(res);
          if (res.data.resultDesc=="支付成功"){
            wx.showModal({
              title: '提示',
              content: '支付成功',
            })
            // var item="";
            // for (var i = 0; i < this.data.arr.length; i++){
            //   item += this.data.arr[i].goodsId + ",";
            // }
            // var oStr = item.substr(0, item.length - 1);
            console.log(this.data.if_00);
            if (this.data.if_00 == undefined) {
              var typeid = "0";
            }else{
              var typeid = this.data.if_00;
            }
            wx.navigateTo({
              url: '/pages/index/order_details/order_details?item=' + this.data.orserid+"&typeid="+typeid,
            })
          }else{
            wx.showModal({
              title: '提示',
              content: '密码错误',
            })
          }
          
        }
      })
    }
  },
  set_Focus() {//聚焦input
    console.log('isFocus', this.data.isFocus)
    this.setData({
      isFocus: true
    })
  },
  set_notFocus() {//失去焦点
    this.setData({
      isFocus: false
    });
    
  },
  close_wallets_password() {//关闭钱包输入密码遮罩
    this.setData({
      isFocus: false,//失去焦点
      wallets_password_flag: false,
    })
  },
  pay() {//去支付
    pay(this)
  }
})
function pay(_this) {
  let apikey = _this.data.apikey;
  let id = _this.data.id;
  let payment_mode = _this.data.payment_mode;
  if (payment_mode == 1) {
    //  微信支付
    // 微信自带密码输入框
    console.log('微信支付')
  } else if (payment_mode == 0) {
    //  转账支付 后续跳转至传转账单照片
    console.log('转账支付')
  } else if (payment_mode == 2) {
    // 钱包支付 输入密码
    console.log('钱包支付')
    _this.setData({
      wallets_password_flag: true,
      isFocus: true
    })
  }

}
// 钱包支付
function wallet_pay(_this) {
  console.log('钱包支付请求函数')
  /*
  1.支付成功
  2.支付失败：提示；清空密码；自动聚焦isFocus:true，拉起键盘再次输入
  */
}